package com.tbit.main.controller;

import com.tbit.main.common.JsonResult;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.main.constant.OperateLog;
import com.tbit.main.exception.BaseException;
import com.tbit.main.pojo.MainCar;
import com.tbit.main.service.MainCarService;
import com.tbit.main.service.TokenService;
import com.tbit.main.util.Assert;
import com.tbit.main.util.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 运维人员车辆管理
 *
 * @author LMY
 * @create 2021-04-07 13:48
 */
@RestController
@RequestMapping("/mainCar")
public class MainCarController {

    @Autowired
    private MainCarService mainCarService;
    @Autowired
    private TokenService tokenService;

    /**
     * 插入
     *
     * @param token
     * @param mainCar
     * @return
     */
    @RequestMapping("/insert")
    @OperateLog(module = ModuleEnum.MAINCAR, methods = MethodEnum.MAINCAR_ADD)
    public JsonResult insert(String token, MainCar mainCar) {
        Assert.hasLength(mainCar.getMachineNO(), "Machine.userCode.notNull");
        Assert.hasLength(mainCar.getMainCarNO(), "Machine.machineNO.notNull");
        Assert.notNull(mainCar.getAccountId(), "AccountUser.agentId.notNull");
        Assert.notNull(mainCar.getAccountId(), "AccountUser.agentId.notNull");

        MainCar mainCarMachineNO = mainCarService.getByMachineNO(mainCar.getMachineNO());

        if (mainCarMachineNO != null) {
            throw new BaseException("Machine.userCode.exist");
        }

        MainCar mainCarNO = mainCarService.getByMainCarNO(mainCar.getMainCarNO());
        if (mainCarNO != null) {
            throw new BaseException("Machine.machineNO.exist");
        }

        mainCar.setAddTime(DateTimeUtil.getNowTime());

        mainCarService.insert(mainCar);

        return JsonResult.succ();
    }

    /**
     * 修改
     *
     * @param token
     * @param mainCar
     * @return
     */
    @RequestMapping("/update")
    @OperateLog(module = ModuleEnum.MAINCAR, methods = MethodEnum.MAINCAR_UPDATE)
    public JsonResult update(String token, MainCar mainCar) {
        Assert.hasLength(mainCar.getMainCarNO(), "Machine.userCode.notNull");
        Assert.hasLength(mainCar.getMachineNO(), "Machine.machineNO.notNull");
        Assert.notNull(mainCar.getMainCarId(), "Machine.machineId.notNull");
        Assert.notNull(mainCar.getAccountId(), "AccountUser.agentId.notNull");
        MainCar mainCarNO = mainCarService.getByMainCarNO(mainCar.getMainCarNO());
        if (mainCarNO != null && !mainCarNO.getMainCarId().equals(mainCar.getMainCarId())) {
            throw new BaseException("Machine.userCode.exist");
        }
        MainCar machineNO = mainCarService.getByMachineNO(mainCar.getMachineNO());
        if (machineNO != null && !machineNO.getMainCarId().equals(mainCar.getMainCarId())) {
            throw new BaseException("Machine.machineNO.exist");
        }


        mainCar.setUpdateTime(DateTimeUtil.getNowTime());
        mainCarService.update(mainCar);

        return JsonResult.succ();
    }

    /**
     * 删除
     *
     * @param token
     * @param mainCarId
     * @return
     */
    @RequestMapping("/del")
    @OperateLog(module = ModuleEnum.MAINCAR, methods = MethodEnum.MAINCAR_DEL)
    public JsonResult del(String token, Integer mainCarId) {
        Assert.notNull(mainCarId, "Machine.machineId.notNull");

        mainCarService.del(mainCarId);
        return JsonResult.succ();
    }

    /**
     * 根据运营区域查询
     *
     * @param token
     * @param accountId
     * @return
     */
    @RequestMapping("/getByAccountId")
    public JsonResult getByAccountId(String token, Integer accountId) {
        Assert.notNull(accountId, "AccountUser.agentId.notNull");
        List<MainCar> mainCars = mainCarService.getByAccountId(accountId);
        return JsonResult.succ(mainCars);
    }

    /**
     * 根据车辆id进行查询
     *
     * @param token
     * @param mainCarId
     * @return
     */
    @RequestMapping("/getByCarId")
    public JsonResult getByCarId(String token, Integer mainCarId) {
        MainCar mainCar = mainCarService.getByMainCarId(mainCarId);
        return JsonResult.succ(mainCar);
    }

    /**
     * 根据车辆NO进行查询
     *
     * @param token
     * @param mainCarNO
     * @return
     */
    @RequestMapping("/getByCarNO")
    public JsonResult getByCarNO(String token, String mainCarNO) {
        MainCar mainCar = mainCarService.getByMainCarNO(mainCarNO);
        return JsonResult.succ(mainCar);
    }
}
